﻿
import YouHP.Graphics.*;

class SingleIndicator extends SingleChartBase
{
	//Y轴刻度值
	var mYLabelArr : Array = new Array();	
	//Y轴上的最小间隔
	var mYGap : Number = 25;					
	
	var mMaxIdxVal = 200;
	var mMinIdxVal = 0;
	
	//计量单位
	var mUnit;
	
	//是否可以量化 
	var mIsNumber : Number;
	
	//记录数目
	var mDataCnt : Number;
	
	function SingleIndicator(lv : Object)
    {
		mClip = _root.createEmptyMovieClip("SingleIndicator", _root.getNextHighestDepth());
        mData = lv;		
		
		mChartLeft = 30 ;
		
		//提取实验室指标公用信息
        ParseBase(lv); 
		
		//提取实验室指标所需要的私有信息
        ParsePrivateData();
				
		//画图
        Draw();
	}
	
	private function ParsePrivateData()
	{
		mChartTop = mLegendTop;
		
		//解析纵坐标值	
		mYLabelArr = mData["Axis"].split(";");		
		
		//计算出纵坐标对应的最大范围值（最大值，最小值）
		if(mYLabelArr.length >= 2)
		{
			mMaxIdxVal = (Number)(mYLabelArr[mYLabelArr.length-1]) + 
				(mYLabelArr[mYLabelArr.length-1] - mYLabelArr[mYLabelArr.length-2]);
			mMinIdxVal = (Number)(mYLabelArr[0]) - 
				(mYLabelArr[mYLabelArr.length-1] - mYLabelArr[mYLabelArr.length-2]);
		}
		
		//解析其它参数
		trace(mData["Var"]);
		var varArr = mData["Var"].split(";");		
		mUnit = varArr[1];
		//判断单位是否为'%'
		if(mUnit == "#")
		{
 			mUnit = "%"			
		}
		
		mIsNumber = varArr[2];	//是否可以量化
		mDataCnt = varArr[3];

		//根据纵坐标计算图表高度
		mChartHeight = mYGap*(mYLabelArr.length + 1);
	}
    
    private function Draw()
    {
		//画网格线以及坐标值
		DrawGrid();

		//画图表数据
        DrawData();        
    } 

	//根据时间获得相对于指标指数图表顶部的Y轴方向的偏移距离
	private function GetYMigration(idxVal:Number)
	{
		var retVal = 0;

		if(idxVal >= mMaxIdxVal)
		{
			return 0;
		}
		else if(idxVal <= mMinIdxVal)
		{
			return mChartHeight;
		}
		else
		{
			retVal = (mMaxIdxVal - idxVal)*1.0/(mMaxIdxVal - mMinIdxVal)*mChartHeight;
		}			
			
		return retVal ;
	}
	
    public function DrawGrid()
    {
       	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mClip);		
		var tmpColor:Number = 0xA0A0FF;
		var gridPen = new Pen(1.0, tmpColor,100);
		var tmpY : Number = mChartTop + mYGap;
		var textGap = 10;
		var curIdx = mYLabelArr.length;
		
		//画网格的矩形边框
		gridPen.setThickness(2);
		gs.DrawRect(gridPen, mChartLeft, mChartTop, mChartRight - mChartLeft, mChartHeight);

		SetXCoordinate(mDosageDate, mUpdateDate);
		for(var i=1; i<mChartXCount; i++)
		{
			if(i%mSegmentCount != 0)
			{
				gridPen.setThickness(0.5);
				gs.DrawDashLine(gridPen, mChartLeft + i*mChartXGap, mChartTop, mChartLeft + i*mChartXGap, mChartTop + mChartHeight, 1, 1);
			}
			else
			{
				gridPen.setThickness(2);
				gs.DrawLine(gridPen, mChartLeft + i*mChartXGap, mChartTop, mChartLeft + i*mChartXGap, mChartTop + mChartHeight);
			}
		}
		
		for(var j=0; j < mYLabelArr.length; j++)	
		{
			//画横向的网格线
			gs.DrawDashLine(gridPen, mChartLeft, tmpY, mChartRight, tmpY, 1, 1);
			//画Y轴坐标值
			DrawText(mYLabelArr[--curIdx], mChartLeft - textGap/2.0, tmpY - textGap, mDefaultColor, 8, "right");
				
			tmpY += mYGap;
		}					
		
		//画坐标值
		for(i=0; i<mChartCoordinate.length; i++)
		{
			DrawText(mChartCoordinate[i][0], mChartLeft + (i+1)*mChartXGap*4, mChartTop + mChartHeight + 10, mDefaultColor, 10, "right");
		}		
    }
	
	private function DrawPoint(pen:Pen, brush:SolidBrush, x:Number, y:Number, radius:Number, value:String, pointCnt:Number)
	{
		var mc:MovieClip = _root.createEmptyMovieClip( "Tip_" + pointCnt, _root.getNextHighestDepth() );
		var gs:Graphics = new Graphics(mc);		
		var link:String = "" ;	//对应的超链接
		
		mc.ToolTip(value, link);
	
		gs.DrawCircle(pen, x, y, radius);
		gs.FillCircle(brush, x, y, radius);		
		
		this.mChildmcArr.push(mc);
	}
	
    public function DrawData()
    {
       	var mc : MovieClip = mClip;
        var gs:Graphics = new Graphics(mc);		
		var pen = new Pen(1.0,0x0000cc,100);		
		var brush = new SolidBrush(0x0000cc, 100);
		
		var dataArr;
		var lastX = 0, curX = 0, lastY = 0, curY = 0;
		var lastVal = -1, curVal = -1;
		var cnt = 0, mRadius = 6;
		var txtTip:String;		
		
		for(var i=0; i<mDataCnt; i++)
		{
			dataArr = mData["Data_" + i].split(";");
			curX = GetXMigration(Utils.StrToDate(dataArr[0]));
			curY = GetYMigration(dataArr[1]) ;
				
			//判断是否第一个点，如果不是，则需要连线
			if(i > 0 && lastX >= 0)
			{
				//画线
				pen.setThickness(4);
				gs.DrawLine(pen, mChartLeft + lastX, mChartTop + lastY, mChartLeft + curX, mChartTop + curY, 1, 1);
			}
				
			//画点
			txtTip = dataArr[0] + "\n" + dataArr[1] + mUnit;
			pen.setThickness(0.5);
			DrawPoint(pen, brush, mChartLeft + curX, mChartTop + curY, mRadius, txtTip, cnt++);
			
			lastX = curX;			
			lastY = curY;
		}		
    } 
	
}